package com.which.one;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;

public class ShaiziActivity extends AppCompatActivity {

    private LinearLayout diceContainer;
    private EditText numberOfDicesEditText;
    private Button rollDiceButton;
    private Random randomGenerator = new Random();
    private TextView diceTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shaizi);

        diceContainer = findViewById(R.id.diceContainer);
        numberOfDicesEditText = findViewById(R.id.numberOfDicesEditText);
        rollDiceButton = findViewById(R.id.rollDiceButton);

        // Initialize the dice text view
        diceTextView = new TextView(this);
        diceTextView.setTextSize(180);
        diceTextView.setTextColor(getResources().getColor(android.R.color.black));
        diceTextView.setGravity(Gravity.CENTER);
        diceTextView.setBackgroundResource(R.drawable.rounded_rectangle);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                720, 720
        );
        params.gravity = Gravity.CENTER; // Center horizontally and vertically within the LinearLayout
        diceTextView.setLayoutParams(params);

        diceContainer.addView(diceTextView);
        diceContainer.setGravity(Gravity.CENTER); // Center all children within the LinearLayout

        rollDiceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputText = numberOfDicesEditText.getText().toString();
                if (!inputText.isEmpty()) {
                    try {
                        int numberOfDices = Integer.parseInt(inputText);
                        if (numberOfDices < 2) {
                            rollSingleDice();
                        } else {
                            showRollResultsDialog(numberOfDices);
                        }
                    } catch (NumberFormatException e) {
                        Toast.makeText(ShaiziActivity.this, "请输入有效的数字", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    rollSingleDice();
                }
            }
        });
    }

    private void rollSingleDice() {
        int result = randomGenerator.nextInt(6) + 1; // Generate a number between 1 and 6
        updateDiceDisplay(result);
    }

    private void updateDiceDisplay(int result) {
        diceTextView.setText(String.valueOf(result));

        // Change text color based on the result
        if (result == 1 || result == 3 || result == 5) {
            diceTextView.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
        } else {
            diceTextView.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
        }
    }

    private void showRollResultsDialog(int numberOfDices) {
        StringBuilder messageBuilder = new StringBuilder();
        float totalSum = 0;

        for (int i = 0; i < numberOfDices; i++) {
            int result = randomGenerator.nextInt(6) + 1;
            messageBuilder.append("色子 ").append(i + 1).append(": ").append(result).append("\n");
            totalSum += result;
        }

        float averageSum = totalSum / numberOfDices;

        messageBuilder.append("\n总点数: ").append(totalSum);
        messageBuilder.append("\n\n平均点数: ").append(averageSum);

        new AlertDialog.Builder(this)
                .setTitle("摇色子结果")
                .setMessage(messageBuilder.toString())
                .setPositiveButton(android.R.string.ok, null)
                .show();
    }
}



